$(function() {
    // 渲染页面-通用部分；
    commTemplUtil.initPage();
    // 渲染左侧菜单;
    commTemplUtil.initCommLeftMemu();

    var addrTemplObj = _.template($('#addr-option-templ').html());
    var categTemplObj = _.template($('#category-templ').html());

    // 地址下拉框选项;
    renderPage(addrTemplObj);
    //当前左边的导航的位置
    commTemplUtil.setLeftMenu(1);

    // 大类/小类, 下拉框选项;
    renderAddcategory(addrTemplObj, categTemplObj);

    var tmpForm = $("#myform1").validate({
        rules: {
            name: "required",
            primary_category_id: "required",
            secondary_category_id: "required",
            unit: "required",
            price: {
                required: true,
                number: true,
            },
            amount: {
                required: true,
                digits: true,
            },
            minimum_order: {
                required: true,
                number: true,
            },
            company: "required",
            title: {
                required: true,
                rangelength: [2, 16],
            }
        },
        success: function() {
            var input_val = $("#offer_1").val();
            if (input_val) {
                $("#tip").attr("style", "display:none");
            }
        },
        messages: {
            title: "",
            primary_category_id: "请选择一个大的类别",
            secondary_category_id: "请选择一个小的的类别",

            amount: "请输入一个整数",
        },
    });

    //tmpForm.form();

});

function renderPage(addrTemplObj) {

    // 省
    $.ajax({
        url: PAGE_CONFIG.requestUrl.addrProvince,
        type: 'get',
        dataType: 'json',
        success: function(result) {
            var datas = $.extend([], result.data);
            var tmplObjHtml = addrTemplObj(datas);
            $("#province").html(tmplObjHtml);

            // typeof PAGE_DATA != 'undefined'
            if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {

                setDefaulAddr("province", PAGE_DATA.addrDefault.province_id || "");
            }
        },
        error: function(xhr, status, error) {
            var tmplObjHtml = addrTemplObj([]);
            $("#province").html(tmplObjHtml);
        }
    });

    $("#province").on("change", function() {
        var code = $(this).val();
        renderAddrCity(code, addrTemplObj);
    });

    $("#city").on("change", function() {
        var code = $(this).val();
        renderAddrCounty(code, addrTemplObj);

    });
};

function renderAddrCity(provinceCode, addrTemplObj) {
    // 市
    $.ajax({
        url: PAGE_CONFIG.requestUrl.addrCity,
        type: 'post',
        dataType: 'json',
        data: {
            code: provinceCode,
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            var tmplObjHtml = addrTemplObj(datas);
            $("#city").html(tmplObjHtml);

            if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
                setDefaulAddr("city", PAGE_DATA.addrDefault.city_id || "");
            }
        },
        error: function(xhr, status, error) {
            var tmplObjHtml = addrTemplObj([]);
            $("#city").html(tmplObjHtml);
        }
    });

    $("#city").empty().html(addrTemplObj([]));
    $("#county").empty().html(addrTemplObj([]));
};

function renderAddrCounty(cityCode, addrTemplObj) {

    // 区/县
    $.ajax({
        url: PAGE_CONFIG.requestUrl.addrCounty,
        type: 'post',
        dataType: 'json',
        data: {
            code: cityCode,
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            var tmplObjHtml = addrTemplObj(datas);
            $("#county").html(tmplObjHtml);

            if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
                setDefaulAddr("county", PAGE_DATA.addrDefault.county_id || "");
            }
        },
        error: function(xhr, status, error) {
            var tmplObjHtml = addrTemplObj([]);
            $("#county").html(tmplObjHtml);
        }
    });
};

function renderAddcategory(addrTemplObj, categTemplObj) {

    $.ajax({
        url: PAGE_CONFIG.requestUrl.offerInfo1,
        type: 'post',
        dataType: 'json',
        data: {
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            var tmplObjHtml = categTemplObj(datas);
            $("#category_1").html(tmplObjHtml);
            $("#category_1").on("change", function() {
                var categorys = $(this).val();
                if (categorys == "") {

                } else {
                    $("#category_2").empty().html(addrTemplObj([]));
                    renderAddcategory_2(categorys, categTemplObj);
                };
            });

            if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
                setDefaulAddr("category_1", PAGE_DATA.addrDefault.category_1 || "");
            }

        },
        error: function(xhr, status, error) {}
    });

};

function renderAddcategory_2(category, categTemplObj) {

    //小类
    $.ajax({
        url: PAGE_CONFIG.requestUrl.offerInfo2,
        type: "post",
        dataType: "json",
        data: {
            id: category,
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            var tmplObjHtml = categTemplObj(datas);
            $("#category_2").html(tmplObjHtml);
            if (typeof PAGE_DATA != 'undefined' && !!PAGE_DATA.addrDefault) {
                setDefaulAddr("category_2", PAGE_DATA.addrDefault.category_2 || "");
            }
        }
    });
};

function setDefaulAddr(domId, value) {

    $("#" + domId).val(value).trigger("change");

};
